package com.gitee.huanminabc.utils_common.obj.reflect;

import java.lang.reflect.Field;

/**
 * @author huanmin
 * @date 2024/1/16
 */
public class EntityFieldsCheck {


    /**
     * 判断对象的所有属性是否都不为空
     * @param obj 对象
     * @param excludeFields 需要排除的字段
     * @return  true:所有属性都不为空, false:有属性为空
     */
    public static boolean allIsNotNull(Object obj, String... excludeFields) {
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        bak:
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            if (excludeFields != null) {
                for (String excludeField : excludeFields) {
                    if (excludeField.equals(name)) {
                        continue bak;
                    }
                }
            }
            try {
                Object o = declaredField.get(obj);
                if (o == null) {
                    return false;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 判断对象的所有属性是否都不为空,如果是String类型,还会判断字符串是否为空
     * @param obj
     * @param excludeFields
     * @return
     */
    public static boolean allIsNotEmpty(Object obj, String... excludeFields) {
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        bak:
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            if (excludeFields != null) {
                for (String excludeField : excludeFields) {
                    if (excludeField.equals(name)) {
                        continue bak;
                    }
                }
            }
            try {
                Object o = declaredField.get(obj);
                if (o == null) {
                    return false;
                }
                if (o instanceof String) {
                    if (((String) o).isEmpty()) {
                        return false;
                    }
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
